//
// C++ Implementation: function2d
//
// Description: 
//
//
// Author:  <>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "function2d.h"
#include "element1d.h"

using namespace CoreLib::MathLib ; 

Function2D::Function2D(const std::string & eq ) : IFunction(eq) , mp_ParserVariable ( new double (0) ), mp_ParserTemporary ( new Element1D(0) )
{
    mp_Parser->DefineVar("x",mp_ParserVariable.get());
}
Function2D::~Function2D()
{
}
std::string Function2D::Equation() const
{
    return IFunction::Equation();
}

const IElement* Function2D::eval(const IElement & x ) const 
{
    Element1D *pValue = (Element1D*)(&x) ; 
    *mp_ParserVariable = pValue->value() ; 
    mp_ParserTemporary->setValue( mp_Parser->Eval() ) ; 
    return mp_ParserTemporary.get() ; 
}

const IElement* Function2D::operator ( )(const IElement & x) const 
{
    return eval(x);
}
void Function2D::setEquation(const std::string& eq)
{
    IFunction::setEquation(eq);
}
/*std::vector< double > & Function2D::Rect(const double & xStart, const double & xStop, const double & xStep) const 
{
    static std::vector< double > ret ; 
    ret.clear();
    for ( double i = xStart; i <=xStop; i+=xStep ) 
        ret.push_back(eval(i));
    return ret ; 
}
*/
double Function2D::eval(const double & x) const
{
    if ( *mp_ParserVariable == x ) 
        return mp_ParserTemporary->value() ; 
    Element1D *pEl = (Element1D*)(eval(Element1D(x)));
    double ret = pEl->value() ; 
    return ret ; 
}
double Function2D::operator ( )(const double & x) const
{
    return eval(x);
}